package sjtu.ist.warehouse4ws.tool;




import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Parameter;

public class ParserFunctionpoint {
	public String pointId;

	public String parseNode(String tagName, Document doc){
		
        NodeList parentlist= doc.getElementsByTagName(tagName);      //获取该ID的标签
     
        Element head= (Element) parentlist.item(0);                 //获取该ID标签的第一个Item
       
        NodeList childlist=head.getChildNodes();                 //获取该ID标签第一个Item里面的结点
		if (childlist.getLength() != 0) {
			String tagvalue = childlist.item(0).getNodeValue(); // 取值
			if (tagvalue.isEmpty()) {
				return null;
			} else {
				return tagvalue;
			}
		} else {
			return null;
		}
		 
	}
	public Functionpoint reservePoint(String path)  {
	
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Functionpoint funPoint = new Functionpoint();
		
		try {
			db = dbf.newDocumentBuilder();
			File file = new File(path);
			Document doc = db.parse(file);
			pointId=parseNode("id",doc);
			String enName=parseNode("enName",doc);
			String cnName=parseNode("cnName",doc);
			String description =parseNode("description",doc);
			
			funPoint.setId(pointId);
			funPoint.setEnName(enName);
			funPoint.setCnName(cnName);
			funPoint.setDescription(description);
			//funPoint.setFileinfo(null);
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return funPoint;
		
		
	}
	public ArrayList<Parameter> reserveParameter(String path) {
	
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		ArrayList<Parameter> list=new ArrayList<Parameter>();
			try {
				db = dbf.newDocumentBuilder();
				File file = new File(path);
				Document doc = null;
				doc = db.parse(file);
				Element root=doc.getDocumentElement();
				NodeList paramlist=root.getElementsByTagName("parameter");
				for(int i=0;i<paramlist.getLength();i++){
					Parameter parameter=new Parameter();
					Element param=(Element) paramlist.item(i);
					
					NodeList idList=param.getElementsByTagName("id");
					String id=getNodeText(idList);
					parameter.setId(id);
					System.out.println(id);
					
					NodeList enNameList=param.getElementsByTagName("enName");
					String enName=getNodeText(enNameList);
					parameter.setEnName(enName);
					
					NodeList cnNameList=param.getElementsByTagName("cnName");
					String cnName=getNodeText(cnNameList);
					parameter.setCnName(cnName);
					
					NodeList descriptionList=param.getElementsByTagName("description");
					String description=getNodeText(descriptionList);
					parameter.setDescription(description);
					
					NodeList inOutTypeList=param.getElementsByTagName("inOutType");
					String inOutType=getNodeText(inOutTypeList);
					parameter.setInOutType(inOutType);
					
					NodeList isBuiltInList=param.getElementsByTagName("isBuiltIn");
					String isBuiltIn=getNodeText(isBuiltInList);
					if( isBuiltIn == "true"){
						parameter.setIsBuiltIn(true);
					}
					if( isBuiltIn == "false"){
						parameter.setIsBuiltIn(false);
					}
					else{
						parameter.setIsBuiltIn(null);
					}
					System.out.println(isBuiltIn);
					
					NodeList paramTypeNameList=param.getElementsByTagName("paramTypeName");
					String paramTypeName=getNodeText(paramTypeNameList);
					parameter.setParamTypeName(paramTypeName);
					
					NodeList paramTypeIdList=param.getElementsByTagName("paramTypeId");
					String paramTypeId=getNodeText(paramTypeIdList);
					parameter.setParamTypeId(paramTypeId);
					
					NodeList collectionTypeList=param.getElementsByTagName("collectionType");
					String collectionType=getNodeText(collectionTypeList);
					parameter.setCollectionType(collectionType);
					
					NodeList restrictionsList=param.getElementsByTagName("restrictions");
					String restrictions=getNodeText(restrictionsList);
					parameter.setRestrictions(restrictions);
					
					parameter.setFunctionPointId(pointId);
					list.add(parameter);
			}
				} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
			
				e.printStackTrace();
			}
	
			return list;

		
	}
	public String getNodeText(NodeList node){
		Element e=(Element)node.item(0);
		if(e==null){return null;}                        //this judge is very important ,without it ,you will found nullpoint Exception
		Text t=(Text)e.getFirstChild();
		if(t==null){return null;}
		String str=t.getNodeValue();
		return str;

	}

	public static void main(String[] args) throws IOException, Exception {
		
		ParserFunctionpoint test=new ParserFunctionpoint();
		String path="E:/Users/chenqidou/Desktop/functions/3a2a53ad-78da-46c0-b35b-95adfd70b66b.xml";
		test.reservePoint(path);
		ArrayList<Parameter> testparam=test.reserveParameter(path);
		for(int i=0;i<testparam.size();i++){
			System.out.println(testparam.get(i).getDescription());
			System.out.println(testparam.get(i).getCnName());
			System.out.println(testparam.get(i).getEnName());
			System.out.println(testparam.get(i).getFunctionPointId());
	
		}



	}

}
